package com.CAS;

import java.util.concurrent.atomic.AtomicInteger;

public class CASDemo {

    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(2020);

        //比较并更新，CAS是CPU的并发原语
        atomicInteger.compareAndSet(2020,2021);
        System.out.println(atomicInteger.get());
        atomicInteger.getAndIncrement();
        //修改失败
        atomicInteger.compareAndSet(2020,2021);
        System.out.println(atomicInteger.get());
    }
}
